package com.line.config.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.line.model.pojo.RedisPojo;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@Configuration
@EnableConfigurationProperties(RedisConfig.class)
@ConfigurationProperties(prefix = "com.line.redis")
public class RedisConfig {

	private Logger log = LoggerFactory.getLogger(RedisConfig.class);

	private RedisPojo redis;

	@Bean
	public RedisConnectionFactory lettuceConnectionFactory() {

		// 加载单机版配置
		RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
		standaloneConfiguration.setDatabase(redis.getDatabase());
		standaloneConfiguration.setHostName(redis.getHost());
		standaloneConfiguration.setPassword(redis.getPassword());
		standaloneConfiguration.setPort(redis.getPort());

		// 加载连接池文件
		GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
		genericObjectPoolConfig.setMaxIdle(redis.getMaxIdle());
		genericObjectPoolConfig.setMinIdle(redis.getMinIdle());
		genericObjectPoolConfig.setMaxTotal(redis.getMaxTotal());
		genericObjectPoolConfig.setMaxWaitMillis(redis.getMaxWait());

		LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration//
				.builder().commandTimeout(Duration.ofSeconds(redis.getTimeout()))//
				.poolConfig(genericObjectPoolConfig)//
				.build();
		// 加载配置工厂
		LettuceConnectionFactory factory = new LettuceConnectionFactory(standaloneConfiguration, clientConfig);
		log.info("RedisConfig info: database={},host={},password={},port={}", factory.getDatabase(), factory.getHostName(), factory.getPassword(),
				factory.getPort());
		return factory;
	}

	@Bean
	public RedisTemplate<String, Object> redisTemplate() {
		// 设置序列化
		Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		// 配置redisTemplate
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(lettuceConnectionFactory());
		RedisSerializer<?> stringSerializer = new StringRedisSerializer();
		redisTemplate.setKeySerializer(stringSerializer);// key序列化
		redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// value序列化
		redisTemplate.setHashKeySerializer(stringSerializer);// Hash key序列化
		redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}

	public RedisPojo getRedis() {
		return redis;
	}

	public void setRedis(RedisPojo redis) {
		this.redis = redis;
	}
}
